package com.huawei.hmsdemo10086.Activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;

import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.huawei.hmsdemo10086.Fragment.HomeFragment;
import com.huawei.hmsdemo10086.Fragment.MyFragment;
import com.huawei.hmsdemo10086.Fragment.VideoFragment;
import com.huawei.hmsdemo10086.R;

public class MainActivity extends AppCompatActivity implements BottomNavigationView.OnNavigationItemSelectedListener{

    //private BottomNavigationView bottomNavigationView;
    private static int ite = R.id.tab_one;
    private NotificationManager notificationManager;
    private Notification notification;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        BottomNavigationView bottomNavigationView = findViewById(R.id.bottomNavigationView);;
        initView(bottomNavigationView);
        //replaceFragment(new EmptyFragment()); //启动时用HomeFragment替换掉空Fragment
    }

    private void initView(BottomNavigationView bottomNavigationView) {
        notification();
        bottomNavigationView.setOnNavigationItemSelectedListener(this);//设置导航栏监听器
        bottomNavigationView.setSelectedItemId(ite);//设置默认选择的导航栏子项tab_one即首页
        bottomNavigationView.setItemIconTintList(null);//取消导航栏子项图片的颜色覆盖
    }

    @Override
    //处理导航栏子项的点击事件
    public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
        //Fragment管理器，用来处理fragment的切换隐藏
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction ft = fragmentManager.beginTransaction();
        int itemId = menuItem.getItemId();//获取点击的位置以及对应的id
        ite = itemId;
        switch (itemId) {
            case R.id.tab_one:
                replaceFragment(new HomeFragment());//遂用HomeFragment替换空Fragment
                menuItem.setChecked(true);
                break;
            case R.id.tab_two:
                replaceFragment(new VideoFragment());
                menuItem.setChecked(true);
                break;
            case R.id.tab_three:
                replaceFragment(new MyFragment());
                menuItem.setChecked(true);
                break;
        }
        return true;
    }
    //    替换Fragment的方法
    private void replaceFragment(Fragment fragment) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.replace(R.id.fragment_empty , fragment);
        transaction.commit();
    }

    public void notification(){
        notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        //设置图片，通知标题，发送时间，提示方式等属性


        if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.O) {
            //创建通知渠道实例（这三个参数是必须要有的）
            NotificationChannel channel = new NotificationChannel("sendMessage","华为推送",NotificationManager.IMPORTANCE_DEFAULT);
            //创建通知渠道的通知管理器
            NotificationManager manager =(NotificationManager)getSystemService(NotificationManager.class);
            //将实例交给管理器
            manager.createNotificationChannel(channel);
        }
        //标题
        Notification notification = new NotificationCompat.Builder(this,"sendMessage")
                .setContentTitle("这是标题")
                .setContentText("这是内容")
                .setWhen(System.currentTimeMillis())
                .setTicker("收到Notification信息")   //收到信息后状态显示的文字信息
                .setWhen(System.currentTimeMillis()) //设置通知时间
                .setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE) //设置默认的三色灯与振动器
                .setAutoCancel(true) //设置点击后取消Notification
                .setSmallIcon(R.drawable.my)
                .build();
        notificationManager.notify(1,notification);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
